import { Category } from 'category-model.js';
import { ProductList } from "../product/list-model.js";
import { Product } from '../product/product-model.js';
import { Cart } from '../cart/cart-model.js';
var category = new Category(); 
var productlist = new ProductList();
var product = new Product();
var cart = new Cart();

Page({
  data: {
    currentMenuIndex: 0,
    sonIndex:'',
    loadingHidden: false,
    hiddenAttrBox: true,
    show_type : 2,  //显示模式 1传统模式 2一级分类模式,3左边大类，右边二级+商品
    cate_id:'',
    pid:''
  },
  onLoad: function () {    
    this._loadData();
  },

  /*加载所有数据*/
  _loadData: function (callback) {
    var that = this;
    var param = {
      num : 100000,
      shop_id: 1
    }
    category.getproductCategoryData(param,(data) => {
      this.setData({
        //show_type: data.show_type ? data.show_type : 2,
        show_type: 3,
        categoryData:data.items,
        loadingHidden:true
      })
      that.product();
    });
  },
  /*加载商品数据*/
  product: function () {
    var that = this;
    var sort = 'id';
    var params ={
      cat_id: this.data.cate_id ? this.data.cate_id : this.data.pid
    }
    if (this.data.loading){
      return false;
    }
    this.data.loading = true;
    productlist.getProductData(this.data.page, sort, params, (data) => {
      this.data.loading = false;
      wx.hideLoading();
      that.setData({
        productData: data.items,
        pageCount: data._meta.pageCount,
        loadingHidden: true,
      })
      if (data._meta.pageCount == 1) {
        that.setData({
          searchLoading: false,
          noMore: true,
        })
      }
    })
  },
  reset:function(){
    this.data.page = 1;
    this.data.productData = [];
  },
  /*切换分类*/
  changeCategory: function (event) {
    var that = this,
    index = category.getDataSet(event, 'index');
    this.data.pid = event.currentTarget.dataset.id;
    that.reset();
    that.product();
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      currentMenuIndex: index,
      sonIndex : '',
      toView: "category_id_" + that.data.categoryData[index].category_id
    });
   },
  /*切换二级分类*/
  change: function (event) {
    var that = this,
    index = category.getDataSet(event, 'index');
    var id = event.currentTarget.dataset.id;
    this.data.cate_id = id;
    that.reset();
    that.product();
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      sonIndex: index,
    });
  },
  /*切换商品属性*/
  onTabsItemTap: function (event) {
    var productDetail = this.data.productDetail;
    var data = product.changAttr(event, productDetail);
    this.setData({
      'productDetail.attributes': data
    });
    var skusindex = product.changProductinf(this.data.productDetail);
    if (skusindex != undefined) {
      this.setData({
        'allselect': 1,
        'skus': this.data.productDetail.skus[skusindex],
      });

    } else {
      this.setData({
        'allselect': 0,
      });
    }

  },

  
  /*跳转到商品列表页*/
  onProductListItemTap: function (event) {
    var id = category.getDataSet(event, 'id');
    var hot = category.getDataSet(event, 'hot');
    // var child = category.getDataSet(event, 'child');
    wx.navigateTo({
      url: '../product/list?id=' + id + '&hot=' + hot 
    })
  },
  
  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = event.currentTarget.dataset.id;
    var skuid = event.currentTarget.dataset.skuid;
    var uid = wx.getStorageSync('uid');
    if (skuid) {
      wx.navigateTo({
        url: '../product/product?id=' + id + '&skuid=' + skuid
      })
    } else {
      wx.navigateTo({
        url: '../product/product?id=' + id
      })
    }
  },
  /*搜索*/
  showInput: function () {
    wx.navigateTo({
      url: '../product/list?search=1'
    })
  },

  //滚动加载
  lower: function (e) {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      productlist.getProductData(page, 20, this.data.params, (data) => {
        this.scrollLoadData(data, page);
      })
    } else {
      this.setData({
        noMore: true,
      })
    }
  },
})